<template>

<div class="mainDiv">        
    <div class="rowHead" style="height: 100%;overflow: auto;">
        <el-col :span="6">
            <el-tag size="small">当前店下员工</el-tag>
            <el-collapse v-model="atv.atvShop">
                <el-collapse-item :name="shop.id">
                    <template slot="title">{{shop.name}}</template>
                    <el-checkbox v-for="(item,index) in shop.child" v-model="item.sele" :key="index">{{item.uname}} / {{item.pname}}</el-checkbox>
                </el-collapse-item>
            </el-collapse>
        </el-col>
        <el-col :span="8">
            <el-tag size="small">部门下员工</el-tag>
            <el-collapse v-model="atv.atvBumen">
                <el-collapse-item :name="b.id" v-for="(b,index) in bumens" :key="index">
                    <template slot="title">{{b.name}}</template>
                    <el-checkbox v-for="(item,index) in b.child" v-model="item.sele" :key="index">{{item.uname}} / {{item.pname}}</el-checkbox>
                </el-collapse-item>
            </el-collapse>
        </el-col>
        <el-col :span="10">
            <el-tag size="small">小组下员工</el-tag>
            <el-collapse v-model="atv.atvGroup">
                <el-collapse-item :name="x.id" v-for="(x,index) in groups" :key="index">
                    <template slot="title">{{x.name}}</template>
                    <el-checkbox v-for="(item,index) in x.child" v-model="item.sele" :key="index">{{item.uname}} / {{item.pname}}</el-checkbox>
                </el-collapse-item>
            </el-collapse>
        </el-col>
   	</div>

   	<div class="rowHead" style="text-align: right;">
        <el-button size="mini" type="primary" icon="el-icon-upload" @click="submit">确定分配员工</el-button>
        <el-button size="mini" type="danger" icon="el-icon-close" onclick="H.closewin()">取消关闭</el-button>
    </div>
</div>
    
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'allotGuest',
  data () {
    return {
      id: 0,
      shop: {},
      bumens: [],
      groups: [],
      atv: {
        atvShop: [],
        atvBumen: [],
        atvGroup: []
      }
    }
  },
  methods: {

    getAllGroups: async function () {
      let that = this
      let res = await C.ajax('/shop/guest/getallgroups', { gid: this.id })
      if (res) {
        res.shop.child = []
        res.users.forEach((v) => {
          if (v.departmentid === res.shop.id) {
            if (v.gid) {
              v.sele = true
              if (that.atv.atvShop.indexOf(res.shop.id) === -1) {
                that.atv.atvShop.push(res.shop.id)
              }
            } else v.sele = false
            res.shop.child.push(v)
          }
        })

        res.bumens.forEach((b) => {
          b.child = []
          res.users.forEach((u) => {
            if (b.id === u.departmentid) {
              if (u.gid) {
                u.sele = true
                if (that.atv.atvBumen.indexOf(b.id) === -1) {
                  that.atv.atvBumen.push(b.id)
                }
              } else u.sele = false
              b.child.push(u)
            }
          })
        })

        res.groups.forEach((x) => {
          x.child = []
          res.users.forEach((u) => {
            if (x.id === u.departmentid) {
              if (u.gid) {
                u.sele = true
                if (that.atv.atvGroup.indexOf(x.id) === -1) {
                  that.atv.atvGroup.push(x.id)
                }
              } else u.sele = false
              x.child.push(u)
            }
          })
        })

        this.shop = res.shop
        this.bumens = res.bumens
        this.groups = res.groups
      }
    },
    // 确定选择，提交
    submit: async function () {
      let uids = []

      this.shop.child.forEach((s) => {
        if (s.sele) {
          uids.push(s.id)
        }
      })

      this.bumens.forEach((b) => {
        b.child.forEach((c) => {
          if (c.sele) {
            uids.push(c.id)
          }
        })
      })

      this.groups.forEach((g) => {
        g.child.forEach((c) => {
          if (c.sele) {
            uids.push(c.id)
          }
        })
      })

      let many = []
      uids.forEach((u) => {
        many.push({
          gid: this.id,
          uid: u
        })
      })

      let data = {
        gid: this.id,
        many: many
      }

      let res = await C.ajax('/shop/guest/allotguest', { data: JSON.stringify(data) }, 'post')
      if (res) {
        C.closeBrowserWindow(res)
      }
    }
  },
  mounted: function () {
    this.id = C.getDatas().id
    this.getAllGroups()
  }
}
</script>